#!/bin/env bash
source ../config
cd $HOME

echo -e ${yellow}正在修改 ${cyan}DNS配置${background}
rm /etc/resolv.conf
echo 'nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 2400:3200::1
nameserver 2400:3200:baba::1' > /etc/resolv.conf
cat /etc/resolv.conf
echo -e ${yellow}已修改为 ${cyan}阿里DNS${background}

echo -e ${yellow}正在修改 ${cyan}Pacman仓库源${background}
echo '
##
## MSYS2 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/$arch
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch
Server = https://mirror.nju.edu.cn/msys2/msys/$arch
Server = https://mirrors.aliyun.com/msys2/msys/$arch
' > mirrorlist.msys

cat > mirrorlist.mingw64 <<- FOX
##
## 32-bit Mingw-w64 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/x86_64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/x86_64
Server = https://mirror.nju.edu.cn/msys2/msys/x86_64
Server = https://mirrors.aliyun.com/msys2/msys/x86_64
FOX

cat > mirrorlist.mingw32 <<- FOX
##
## 32-bit Mingw-w64 repository mirrorlist
##

## Primary
## msys2.org
Server = https://mirrors.bfsu.edu.cn/msys2/msys/i686
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/i686
Server = https://mirror.nju.edu.cn/msys2/msys/i686
Server = https://mirrors.aliyun.com/msys2/msys/i686
FOX

cat > mirrorlist.clang64 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/clang64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/clang64
Server = https://mirror.nju.edu.cn/msys2/mingw/clang64
Server = https://mirrors.aliyun.com/msys2/mingw/clang64
FOX

cat > mirrorlist.clang32 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/clang32
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/clang32
Server = https://mirror.nju.edu.cn/msys2/mingw/clang32
Server = https://mirrors.aliyun.com/msys2/mingw/clang32
FOX

cat > mirrorlist.ucrt64 <<- FOX
## Primary
Server = https://mirrors.bfsu.edu.cn/msys2/mingw/ucrt64
Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/ucrt64
Server = https://mirror.nju.edu.cn/msys2/mingw/ucrt64
Server = https://mirrors.aliyun.com/msys2/mingw/ucrt64
FOX

mv -f mirrorlist.msys /etc/pacman.d/mirrorlist.msys
mv -f mirrorlist.mingw64 /etc/pacman.d/mirrorlist.mingw64
mv -f mirrorlist.mingw32 /etc/pacman.d/mirrorlist.mingw32
mv -f mirrorlist.clang64 /etc/pacman.d/mirrorlist.clang64
mv -f mirrorlist.clang32 /etc/pacman.d/mirrorlist.clang32
mv -f mirrorlist.ucrt64 /etc/pacman.d/mirrorlist.ucrt64
cat /etc/pacman.d/mirrorlist.msys
echo -e ${yellow}修改完成${background}

echo -e ${yellow}正在更新 ${cyan}MSYS2${background}
if pacman -Syyu --noconfirm --needed
then
  echo -e ${yellow}更新完成${background}
else
  ErrorReturn
fi

echo -e ${yellow}正在安装 ${cyan}依赖软件${background}
if pacman -S --needed --noconfirm tar gzip xz tmux curl wget pv git unzip dialog
then
  echo -e ${cyan}安装完成${background}
else
  ErrorReturn
fi

if [ ! -d $HOME/AFHOME/chrome-win ]
then
  echo -e ${yellow}开始安装 ${cyan}chromium${background}
  ChromiumURL="https://registry.npmmirror.com/-/binary/playwright/builds/chromium/1124/chromium-win64.zip"
  if ! Download chromium.zip ${ChromiumURL}
  then
    ErrorReturn
    exit 1
  fi
  echo -e ${yellow}正在解压 ${cyan}chromium.zip${background}
  if unzip -q chromium.zip
  then
    rm chromium.zip
    echo -e ${green}解压完成${background}
  else
    rm chromium.zip
    echo -e ${red}解压失败${background}
    ErrorReturn
    exit
  fi
fi

if [ ! -e /usr/local/Redis/redis-server.exe ]
then
  RedisURL="${GithubMirror}https://github.com/tporadowski/redis/releases/download/v5.0.14.1/Redis-x64-5.0.14.1.zip"
  Download Redis-Server ${RedisURL} -d /usr/local/Redis/
fi

if [ ! -d /usr/local/NodeJS/bin ]
then
echo -e ${yellow}开始安装 ${cyan}NodeJS${background}
echo -e ${yellow}正在获取 ${cyan}NodeJS版本信息${background}
until NodeVersion="$(curl https://mirrors.ustc.edu.cn/node/latest-iron/ | grep .pkg | awk -F 'node-|\.pkg' '{print $2}')"
do
  echo -e ${red}获取失败 ${yellow}三秒后重试${background}
  sleep 3s
done

NodeJSURL="https://registry.npmmirror.com/-/binary/node/latest-iron/node-${NodeVersion}-win-x64.zip"

if Download NodeJS.zip ${NodeJSURL}
then
  echo -e ${yellow}正在解压 ${cyan}NodeJS.zip${background}
  unzip 
else
  ErrorReturn
  exit 1
fi

mkdir NodeJS
if unzip -q NodeJS.zip -d NodeJS
then
  echo -e ${green}解压完成${background}
else
  ErrorReturn
fi
rm -rvf NodeJS.zip
rm -rf /usr/local/NodeJS > /dev/null 2>&1
mv NodeJS/$(ls NodeJS) /usr/local/NodeJS
rm -rf NodeJS
fi

echo -e ${yellow}正在设置NPM ${cyan}默认仓库${background}
npm config set registry https://registry.npmmirror.com
npm config set registry https://registry.npmmirror.com
npm install -g npm@latest
echo -e ${yellow}已设置为 ${cyan}npmmirror${background}

echo -e ${yellow}正在安装 ${cyan}YARN${background}
npm install -g yarn
echo -e ${yellow}正在设置PNPM ${cyan}默认仓库${background}
yarn config set registry https://registry.npmmirror.com
yarn config set registry https://registry.npmmirror.com
echo -e ${green}已设置为 ${yellow}npmmirror${background}

echo -e ${green}正在安装 ${yellow}PNM2${background}
until npm install -g pm2
do
  echo -e ${cyan}安装失败 ${yellow}三秒后重试${background}
  sleep 3s
done
echo -e ${green}安装完成${background}

if ! grep -q "AFHOME" /etc/profile
then
  echo '
  PATH=$PATH:$HOME/AFHOME/
  PATH=$PATH:$HOME/AFHOME/chrome-win/
  PATH=$PATH:/usr/local/NodeJS/bin/
  PATH=$PATH:/usr/local/Redis/
  ' >> /etc/profile
fi

echo -e ${green}开始安装Yunzai-Next Script${background}
if ! git clone --depth=1 ${GithubMirror}https://github.com/ArcticFox520/Yunzai-Next-Script ./AFYN
then
  echo -e ${red} - ${yellow}出现错误${background}
  exit
fi
cd AFYN
chmod +x afyn
cp -f afyn /usr/bin/afyn
AF_HOME_Path="$(pwd)"
sed -i "s|AF_HOME_Path|${AF_HOME_Path}|g" /usr/bin/afyn
if ! grep -q "AFYN" /etc/profile
then
  echo 'PATH=$PATH:$HOME/AFHOME/AFYN/' >> /etc/profile
fi
export AFHOME=${AF_HOME_Path}